package vn.com.shoppie.object;

import java.util.ArrayList;

import vn.com.shoppie.activity.R;
import vn.com.shoppie.sobject.Merchant;
import vn.com.shoppie.sobject.ShoppieObject;
import vn.com.shoppie.sobject.Store;
import vn.com.shoppie.sobjmng.MerchantMng;
import vn.com.shoppie.util.BitmapWorkerTask;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class StoreAdapter extends BaseAdapter implements BitmapWorkerTask.OnLoadBitmapHandle{
	LayoutInflater inflater;
	private Context ctx;
	int imageBackground;
	ArrayList<ShoppieObject> mData;
	MerchantMng mcMng;
	ArrayList<Merchant> data;
	public StoreAdapter(Context c, ArrayList<ShoppieObject> objects) {
		ctx = c;
		this.mData = objects;
		inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		TypedArray ta = c.obtainStyledAttributes(R.styleable.Gallery1);
		imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
		ta.recycle();
		
		mcMng = new MerchantMng(ctx);
		data = mcMng.select();
	}

	@Override
	public int getCount() {
		return mData.size();
	}

	@Override
	public Object getItem(int arg0) {

		return arg0;
	}

	@Override
	public long getItemId(int arg0) {

		return arg0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			ItemShopHolder holder = new ItemShopHolder();
			v = LayoutInflater.from(ctx).inflate(R.layout.item_store, parent, false);
			holder.lyAvatar = (LinearLayout) v.findViewById(R.id.ly_avatar);

			holder.avatar = (ImageView) v.findViewById(R.id.imv_thumbnail);

			holder.title = (TextView) v.findViewById(R.id.tv_name);
			holder.desc = (TextView) v.findViewById(R.id.tv_address);
			holder.distance=(TextView)v.findViewById(R.id.tv_distance);

			v.setTag(holder);
		}

		final ItemShopHolder holder = (ItemShopHolder) v.getTag();
		fillShopToView(holder, mData.get(position));

		OnClickListener _listener = new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (listener != null) {
					listener.itemClicked(holder, mData.get(position));
				}
			}
		};

		holder.avatar.setOnClickListener(_listener);
		holder.desc.setOnClickListener(_listener);
		holder.title.setOnClickListener(_listener);

		return v;
	}

	private void fillShopToView(final ItemShopHolder holder, final ShoppieObject item1) {
		if (item1 instanceof Store) {
			final Store store = (Store) item1;
			if (((Store) item1).thumbnail == null || ((Store) item1).thumbnail.equals("")) {
				if (data != null) {
					for (Merchant item : data) {
						try {
							if (item.merchId.equals(store.merchId)) {
								store.thumbnail = item.merchImage;
								break;
							}
						} catch (NullPointerException e) { }
					}
				}
			}
			if(((Store)item1).thumbnail!=null){
				BitmapWorkerTask.loadBitmap(ctx,StoreAdapter.this, holder, ((Store)item1).thumbnail,item1);
			}
			
			holder.title.setText(store.storeName);
			holder.desc.setText(store.storeAddress);
			holder.distance.setText(store.getDistance());
		}
	}

	public void setOnStoreItemClickListener(OnStoreItemListener listener) {
		this.listener = listener;
	}

	class ItemShopHolder extends BitmapWorkerTask.BitmapHolder{
		LinearLayout lyAvatar;
		ImageView avatar;
		TextView title;
		TextView desc;
		Bitmap bmpHolder;
		TextView distance;
		@Override
		public ImageView getImageView() {
			return avatar;
		}
		
		@Override
		public Bitmap getBitmapHolder() {
			return bmpHolder;
		}
		
	}

	OnStoreItemListener listener = null;

	public interface OnStoreItemListener {
		public void itemClicked(ItemShopHolder holder, ShoppieObject item);
	}

	@Override
	public Bitmap loadBitmapInBackground(Object... object) {
		if(object.length==2){
			String thumb=object[0].toString();
			if(object[1] instanceof Store){
				return ((Store)object[1]).getBitmap(ctx, thumb);
			}
		}
		return null;
	}

}